package com.khyr.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.khyr.dao.*;
import com.khyr.entity.*;
import com.khyr.dao.*;
import com.khyr.entity.Dept;
import com.khyr.entity.HisCareHistory;
import com.khyr.entity.HisPatient;
import com.khyr.service.UserService;
import com.khyr.vo.GetUserListByDeptIdQDto;
import com.khyr.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private HisCareHistoryDao hisCareHistoryDao;
    @Autowired
    private GetUserListByDeptIdQDtoDao getUserListByDeptIdQDtoDao;
    @Autowired
    private DeptDao deptDao;
    @Autowired
    private HisPatientDao hisPatientDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private HisOrderChargeDao hisOrderChargeDao;
    @Autowired
    private HisOrderChangeItemDao hisOrderChargeItemDao;
    @Override
    public R<User> getByName(String phone) {
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("phone",phone);
        User user = userDao.selectOne(queryWrapper);
        if(user==null){
            return new R<>(500,"账号不存在",null);
        }
        if(user.getStatus().equals("1")){
            return new R<>(500,"账号异常",null);
        }
        if(user.getDelFlag().equals("1")){
            return new R<>(500,"账号已注销",null);
        }
        return new R<User>(200,"登录成功",user);
    }
    @Override
    public R<HisPatient> getByPatientName(String toString) {
        QueryWrapper<HisPatient> wrapper=new QueryWrapper<>();
        wrapper.eq("phone",toString);
        return new R<HisPatient>(200,"查询成功",hisPatientDao.selectOne(wrapper));
    }

    @Override
    public R<List<User>> getUserListRanges() {
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.eq("scheduling_flag","0");
        wrapper.eq("del_flag","0");
        List<User> users = userDao.selectList(wrapper);
        return new R<List<User>>(200,"查询成功",users);
    }

    @Override
    public R<List<User>> getUserListByDeptId(Integer deptId) {
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.eq("del_flag","0");
        wrapper.eq("status","0");
        wrapper.eq("scheduling_flag","0");
        if(deptId!=null){
            wrapper.eq("dept_id",deptId);
        }
        List<User> p = userDao.selectList(wrapper);
        for(User user : p){
            QueryWrapper<Dept> deptWrapper=new QueryWrapper<>();
            deptWrapper.eq("dept_id",user.getDeptId());
            Dept dept = deptDao.selectOne(deptWrapper);
            user.setDeptName(dept.getDeptName());
        }
        return new R<>(200,"查询成功",p);
    }

    @Override
    public R<GetUserListByDeptIdQDto> getUserByIdQ(Long id) {
        QueryWrapper<GetUserListByDeptIdQDto> wrapper=new QueryWrapper<>();
        wrapper.eq("user_id",id);
        wrapper.eq("del_flag","0");
        GetUserListByDeptIdQDto user = getUserListByDeptIdQDtoDao.selectOne(wrapper);
        return new R<>(200,"查询成功",user);
    }
    @Override
    public R<User> getUserByChId(String chId) {
        // 根据病例ID（chId）查询医生id
        QueryWrapper<HisCareHistory> wrapper=new QueryWrapper<>();
        wrapper.eq("ch_id",chId);
        HisCareHistory hisCareHistory = hisCareHistoryDao.selectOne(wrapper);
        // 根据医生id查询医生信息
        QueryWrapper<User> userWrapper=new QueryWrapper<>();
        userWrapper.eq("user_id",hisCareHistory.getUserId());
        User user = userDao.selectOne(userWrapper);
        if(user==null){
            return new R<>(500,"查询失败",null);
        }else {
            return new R<User>(200,"查询成功",user);
        }
    }

    @Override
    public R<List<User>> getAllSchedulingList() {
        // 查询排班医生
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.eq("del_flag","0");
        wrapper.eq("status","0");
        wrapper.eq("scheduling_flag","0");
        List<User> users = userDao.selectList(wrapper);
        return new R<List<User>>(200,"查询成功",users);
    }

    @Override
    public R<List<User>> getSchedulingDoctorByCon(Map<String, Object> map) {
        System.out.println(map+"================================================");
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.eq("del_flag","0");
        wrapper.eq("status","0");
        wrapper.eq("scheduling_flag","0");
        //Page<User> page = new Page<>(Long.valueOf(String.valueOf(map.get("pageNum")) ), Long.valueOf(String.valueOf(map.get("pageSize")) ));

        if(map.get("deptId")!=null){
            wrapper.eq("dept_id",map.get("deptId"));
        }
        if(map.get("userId")!=null){
            wrapper.eq("user_id",map.get("userId"));
        }
        List<User> p = userDao.selectList(wrapper);
        return new R<>(200,"查询成功",p);
    }


}
